home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Parser / parser.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  8.1 KB  |  399 lines

  1. /* Parser implementation */
  2.  
  3. /* For a description, see the comments at end of this file */
  4.  
  5. /* XXX To do: error recovery */
  6.  
  7. #include "pgenheaders.h"
  8. #include "assert.h"
  9. #include "token.h"
  10. #include "grammar.h"
  11. #include "node.h"
  12. #include "parser.h"
  13. #include "errcode.h"
  14.  
  15.  
  16. #ifdef Py_DEBUG
  17. extern int Py_DebugFlag;
  18. #define D(x) if (!Py_DebugFlag); else x
  19. #else
  20. #define D(x)
  21. #endif
  22.  
  23.  
  24. /* STACK DATA TYPE */
  25.  
  26. static void s_reset Py_PROTO((stack *));
  27.  
  28. static void
  29. s_reset(s)
  30.     stack *s;
  31. {
  32.     s->s_top = &s->s_base[MAXSTACK];
  33. }
  34.  
  35. #define s_empty(s) ((s)->s_top == &(s)->s_base[MAXSTACK])
  36.  
  37. static int s_push Py_PROTO((stack *, dfa *, node *));
  38.  
  39. static int
  40. s_push(s, d, parent)
  41.     register stack *s;
  42.     dfa *d;
  43.     node *parent;
  44. {
  45.     register stackentry *top;
  46.     if (s->s_top == s->s_base) {
  47.         fprintf(stderr, "s_push: parser stack overflow\n");
  48.         return -1;
  49.     }
  50.     top = --s->s_top;
  51.     top->s_dfa = d;
  52.     top->s_parent = parent;
  53.     top->s_state = 0;
  54.     return 0;
  55. }
  56.  
  57. #ifdef Py_DEBUG
  58.  
  59. static void s_pop Py_PROTO((stack *));
  60.  
  61. static void
  62. s_pop(s)
  63.     register stack *s;
  64. {
  65.     if (s_empty(s))
  66.         Py_FatalError("s_pop: parser stack underflow -- FATAL");
  67.     s->s_top++;
  68. }
  69.  
  70. #else /* !Py_DEBUG */
  71.  
  72. #define s_pop(s) (s)->s_top++
  73.  
  74. #endif
  75.  
  76.  
  77. /* PARSER CREATION */
  78.  
  79. parser_state *
  80. PyParser_New(g, start)
  81.     grammar *g;
  82.     int start;
  83. {
  84.     parser_state *ps;
  85.     
  86.     if (!g->g_accel)
  87.         PyGrammar_AddAccelerators(g);
  88.     ps = PyMem_NEW(parser_state, 1);
  89.     if (ps == NULL)
  90.         return NULL;
  91.     ps->p_grammar = g;
  92.     ps->p_tree = PyNode_New(start);
  93.     if (ps->p_tree == NULL) {
  94.         PyMem_DEL(ps);
  95.         return NULL;
  96.     }
  97.     s_reset(&ps->p_stack);
  98.     (void) s_push(&ps->p_stack, PyGrammar_FindDFA(g, start), ps->p_tree);
  99.     return ps;
  100. }
  101.  
  102. void
  103. PyParser_Delete(ps)
  104.     parser_state *ps;
  105. {
  106.     /* NB If you want to save the parse tree,
  107.        you must set p_tree to NULL before calling delparser! */
  108.     PyNode_Free(ps->p_tree);
  109.     PyMem_DEL(ps);
  110. }
  111.  
  112.  
  113. /* PARSER STACK OPERATIONS */
  114.  
  115. static int shift Py_PROTO((stack *, int, char *, int, int));
  116.  
  117. static int
  118. shift(s, type, str, newstate, lineno)
  119.     register stack *s;
  120.     int type;
  121.     char *str;
  122.     int newstate;
  123.     int lineno;
  124. {
  125.     assert(!s_empty(s));
  126.     if (PyNode_AddChild(s->s_top->s_parent, type, str, lineno) == NULL) {
  127.         fprintf(stderr, "shift: no mem in addchild\n");
  128.         return -1;
  129.     }
  130.     s->s_top->s_state = newstate;
  131.     return 0;
  132. }
  133.  
  134. static int push Py_PROTO((stack *, int, dfa *, int, int));
  135.  
  136. static int
  137. push(s, type, d, newstate, lineno)
  138.     register stack *s;
  139.     int type;
  140.     dfa *d;
  141.     int newstate;
  142.     int lineno;
  143. {
  144.     register node *n;
  145.     n = s->s_top->s_parent;
  146.     assert(!s_empty(s));
  147.     if (PyNode_AddChild(n, type, (char *)NULL, lineno) == NULL) {
  148.         fprintf(stderr, "push: no mem in addchild\n");
  149.         return -1;
  150.     }
  151.     s->s_top->s_state = newstate;
  152.     return s_push(s, d, CHILD(n, NCH(n)-1));
  153. }
  154.  
  155.  
  156. /* PARSER PROPER */
  157.  
  158. static int classify Py_PROTO((grammar *, int, char *));
  159.  
  160. static int
  161. classify(g, type, str)
  162.     grammar *g;
  163.     register int type;
  164.     char *str;
  165. {
  166.     register int n = g->g_ll.ll_nlabels;
  167.     
  168.     if (type == NAME) {
  169.         register char *s = str;
  170.         register label *l = g->g_ll.ll_label;
  171.         register int i;
  172.         for (i = n; i > 0; i--, l++) {
  173.             if (l->lb_type == NAME && l->lb_str != NULL &&
  174.                     l->lb_str[0] == s[0] &&
  175.                     strcmp(l->lb_str, s) == 0) {
  176.                 D(printf("It's a keyword\n"));
  177.                 return n - i;
  178.             }
  179.         }
  180.     }
  181.     
  182.     {
  183.         register label *l = g->g_ll.ll_label;
  184.         register int i;
  185.         for (i = n; i > 0; i--, l++) {
  186.             if (l->lb_type == type && l->lb_str == NULL) {
  187.                 D(printf("It's a token we know\n"));
  188.                 return n - i;
  189.             }
  190.         }
  191.     }
  192.     
  193.     D(printf("Illegal token\n"));
  194.     return -1;
  195. }
  196.  
  197. int
  198. PyParser_AddToken(ps, type, str, lineno)
  199.     register parser_state *ps;
  200.     register int type;
  201.     char *str;
  202.     int lineno;
  203. {
  204.     register int ilabel;
  205.     
  206.     D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str));
  207.     
  208.     /* Find out which label this token is */
  209.     ilabel = classify(ps->p_grammar, type, str);
  210.     if (ilabel < 0)
  211.         return E_SYNTAX;
  212.     
  213.     /* Loop until the token is shifted or an error occurred */
  214.     for (;;) {
  215.         /* Fetch the current dfa and state */
  216.         register dfa *d = ps->p_stack.s_top->s_dfa;
  217.         register state *s = &d->d_state[ps->p_stack.s_top->s_state];
  218.         
  219.         D(printf(" DFA '%s', state %d:",
  220.             d->d_name, ps->p_stack.s_top->s_state));
  221.         
  222.         /* Check accelerator */
  223.         if (s->s_lower <= ilabel && ilabel < s->s_upper) {
  224.             register int x = s->s_accel[ilabel - s->s_lower];
  225.             if (x != -1) {
  226.                 if (x & (1<<7)) {
  227.                     /* Push non-terminal */
  228.                     int nt = (x >> 8) + NT_OFFSET;
  229.                     int arrow = x & ((1<<7)-1);
  230.                     dfa *d1 = PyGrammar_FindDFA(
  231.                         ps->p_grammar, nt);
  232.                     if (push(&ps->p_stack, nt, d1,
  233.                         arrow, lineno) < 0) {
  234.                         D(printf(" MemError: push\n"));
  235.                         return E_NOMEM;
  236.                     }
  237.                     D(printf(" Push ...\n"));
  238.                     continue;
  239.                 }
  240.                 
  241.                 /* Shift the token */
  242.                 if (shift(&ps->p_stack, type, str,
  243.                         x, lineno) < 0) {
  244.                     D(printf(" MemError: shift.\n"));
  245.                     return E_NOMEM;
  246.                 }
  247.                 D(printf(" Shift.\n"));
  248.                 /* Pop while we are in an accept-only state */
  249.                 while (s = &d->d_state
  250.                         [ps->p_stack.s_top->s_state],
  251.                     s->s_accept && s->s_narcs == 1) {
  252.                     D(printf("  Direct pop.\n"));
  253.                     s_pop(&ps->p_stack);
  254.                     if (s_empty(&ps->p_stack)) {
  255.                         D(printf("  ACCEPT.\n"));
  256.                         return E_DONE;
  257.                     }
  258.                     d = ps->p_stack.s_top->s_dfa;
  259.                 }
  260.                 return E_OK;
  261.             }
  262.         }
  263.         
  264.         if (s->s_accept) {
  265.             /* Pop this dfa and try again */
  266.             s_pop(&ps->p_stack);
  267.             D(printf(" Pop ...\n"));
  268.             if (s_empty(&ps->p_stack)) {
  269.                 D(printf(" Error: bottom of stack.\n"));
  270.                 return E_SYNTAX;
  271.             }
  272.             continue;
  273.         }
  274.         
  275.         /* Stuck, report syntax error */
  276.         D(printf(" Error.\n"));
  277.         return E_SYNTAX;
  278.     }
  279. }
  280.  
  281.  
  282. #ifdef Py_DEBUG
  283.  
  284. /* DEBUG OUTPUT */
  285.  
  286. void
  287. dumptree(g, n)
  288.     grammar *g;
  289.     node *n;
  290. {
  291.     int i;
  292.     
  293.     if (n == NULL)
  294.         printf("NIL");
  295.     else {
  296.         label l;
  297.         l.lb_type = TYPE(n);
  298.         l.lb_str = STR(n);
  299.         printf("%s", PyGrammar_LabelRepr(&l));
  300.         if (ISNONTERMINAL(TYPE(n))) {
  301.             printf("(");
  302.             for (i = 0; i < NCH(n); i++) {
  303.                 if (i > 0)
  304.                     printf(",");
  305.                 dumptree(g, CHILD(n, i));
  306.             }
  307.             printf(")");
  308.         }
  309.     }
  310. }
  311.  
  312. void
  313. showtree(g, n)
  314.     grammar *g;
  315.     node *n;
  316. {
  317.     int i;
  318.     
  319.     if (n == NULL)
  320.         return;
  321.     if (ISNONTERMINAL(TYPE(n))) {
  322.         for (i = 0; i < NCH(n); i++)
  323.             showtree(g, CHILD(n, i));
  324.     }
  325.     else if (ISTERMINAL(TYPE(n))) {
  326.         printf("%s", _PyParser_TokenNames[TYPE(n)]);
  327.         if (TYPE(n) == NUMBER || TYPE(n) == NAME)
  328.             printf("(%s)", STR(n));
  329.         printf(" ");
  330.     }
  331.     else
  332.         printf("? ");
  333. }
  334.  
  335. void
  336. printtree(ps)
  337.     parser_state *ps;
  338. {
  339.     if (Py_DebugFlag) {
  340.         printf("Parse tree:\n");
  341.         dumptree(ps->p_grammar, ps->p_tree);
  342.         printf("\n");
  343.         printf("Tokens:\n");
  344.         showtree(ps->p_grammar, ps->p_tree);
  345.         printf("\n");
  346.     }
  347.     printf("Listing:\n");
  348.     PyNode_ListTree(ps->p_tree);
  349.     printf("\n");
  350. }
  351.  
  352. #endif /* Py_DEBUG */
  353.  
  354. /*
  355.  
  356. Description
  357. -----------
  358.  
  359. The parser's interface is different than usual: the function addtoken()
  360. must be called for each token in the input.  This makes it possible to
  361. turn it into an incremental parsing system later.  The parsing system
  362. constructs a parse tree as it goes.
  363.  
  364. A parsing rule is represented as a Deterministic Finite-state Automaton
  365. (DFA).  A node in a DFA represents a state of the parser; an arc represents
  366. a transition.  Transitions are either labeled with terminal symbols or
  367. with non-terminals.  When the parser decides to follow an arc labeled
  368. with a non-terminal, it is invoked recursively with the DFA representing
  369. the parsing rule for that as its initial state; when that DFA accepts,
  370. the parser that invoked it continues.  The parse tree constructed by the
  371. recursively called parser is inserted as a child in the current parse tree.
  372.  
  373. The DFA's can be constructed automatically from a more conventional
  374. language description.  An extended LL(1) grammar (ELL(1)) is suitable.
  375. Certain restrictions make the parser's life easier: rules that can produce
  376. the empty string should be outlawed (there are other ways to put loops
  377. or optional parts in the language).  To avoid the need to construct
  378. FIRST sets, we can require that all but the last alternative of a rule
  379. (really: arc going out of a DFA's state) must begin with a terminal
  380. symbol.
  381.  
  382. As an example, consider this grammar:
  383.  
  384. expr:    term (OP term)*
  385. term:    CONSTANT | '(' expr ')'
  386.  
  387. The DFA corresponding to the rule for expr is:
  388.  
  389. ------->.---term-->.------->
  390.     ^          |
  391.     |          |
  392.     \----OP----/
  393.  
  394. The parse tree generated for the input a+b is:
  395.  
  396. (expr: (term: (NAME: a)), (OP: +), (term: (NAME: b)))
  397.  
  398. */
  399.